package com.ol.edu.server.service.course.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ol.edu.server.entity.Course;
import com.ol.edu.server.entity.CourseVO;
import com.ol.edu.server.entity.Teacher;
import com.ol.edu.server.mapper.CourseMapper;
import com.ol.edu.server.service.course.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Roger
 * @date 2020/9/5 14:13
 * @description
 */
@Service
@Transactional(rollbackFor = Exception.class)//默认是遇到运行时异常时才回滚
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /*
       分页获取课程列表数据&多条件查询
    */
    @Override
    public PageInfo<Course> findCourseByConditioin(CourseVO courseVo) {
        //todo 看下前端的数据展示方式再决定要不要加分页
        PageHelper.startPage(courseVo.getCurrentPage(),courseVo.getPageSize());
        List<Course> courseList = courseMapper.findCourseByConditioin(courseVo);
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);

        return pageInfo;
    }

    /*
        新建课程
     */
    @Override
    public void addCourse(CourseVO courseVo) {
            Course course = new Course();
            Teacher teacher = new Teacher();
            Date date = new Date();
            //因为课程数据中也包含了讲师信息，所以要把数据也存到讲师表中
            BeanUtils.copyProperties(courseVo, course);
            course.setIsDel(0);
            //保存课程信息
            courseMapper.saveCourse(course);
            //保存成功后，获取保存后的课程的ID
            Integer id = course.getId();

            BeanUtils.copyProperties(courseVo, teacher);
            //由于讲师关联的是课程ID所以也要保存课程ID
            teacher.setCourseId(id);
            teacher.setIsDel(0);
            //保存讲师信息
            courseMapper.saveTeacher(teacher);

    }
    /*
        更新课程
     */
    @Override
    public void updateCourse(CourseVO courseVo) {
        Course course = new Course();
        Teacher teacher = new Teacher();

        //更新课程信息
        BeanUtils.copyProperties(courseVo, course);

        courseMapper.updateCourse(course);
        //更新讲师信息
        BeanUtils.copyProperties(courseVo, teacher);

        teacher.setCourseId(courseVo.getId());
        courseMapper.updateTeacher(teacher);

    }

    /*
        根据id查询课程信息
     */
    @Override
    public CourseVO findCourseById(Integer id) {
        if (id == null) {
            return null;
        }
        //id不为空时查询
        CourseVO courseVO = courseMapper.findCourseById(id);
        return courseVO;
    }
    /*
        修改课程状态
     */
    @Override
    public Map<String, Integer> updateCourseStatus(Integer id, Integer status) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(status);

        Integer res = courseMapper.updateCourseStatus(course);
        Map<String, Integer> map = new HashMap<>(1);
        //表示更新成功
        if (res == 1) {
            map.put("status", status);
            return map;
        }
        return null;
    }
}
